//
//  SunDelegateWebVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/30.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunDelegateWebVC.h"
#import "OpenBindSuccessViewController.h"
#import "SunRecordVC.h"
#import "SunMyOrderVC.h"
#import "ResultViewController.h"
#import "InvestSuccessViewController.h"
@interface SunDelegateWebVC ()<UIWebViewDelegate>
{
    UIWebView *_web;
}
@end

@implementation SunDelegateWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _web = [[UIWebView alloc]init];
    [self.view addSubview:_web];
    [_web lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    _web.delegate = self;
    BOOL HaveAccessID = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        HaveAccessID = YES;
    }
    if (!self.params) {
        self.params = @"";
    }
    [HttpRequest postRequestXieyiURLStr:self.urlStr withParameter:self.params HaveAccessID:HaveAccessID IsHTML:NO success:^(id reresponseData) {
        [_web loadHTMLString:[[NSString alloc] initWithData:reresponseData encoding:NSUTF8StringEncoding] baseURL:nil];
        
    }];
    [[Sun shareSun]showActivity:self];
    // Do any additional setup after loading the view.
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [[Sun shareSun]hideAll];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *url = [request.URL absoluteString];
    if ([url containsString:@"action=11"]) {
        ResultViewController *resultVC = [[ResultViewController alloc]init];
        resultVC.type = Recharge;
        NSRange rechargeRange = [url rangeOfString:@"rechargeId"];
        resultVC.borrowId = [url substringFromIndex:rechargeRange.location + rechargeRange.length + 1];
        [self.navigationController pushViewController:resultVC animated:YES];
        return NO;
    }
    if ([url containsString:@"action=13"]) {
        ResultViewController *resultVC = [[ResultViewController alloc]init];
        resultVC.type = GetCash;
        NSRange rechargeRange = [url rangeOfString:@"cashId"];
        resultVC.borrowId = [url substringFromIndex:rechargeRange.location + rechargeRange.length + 1];
        [self.navigationController pushViewController:resultVC animated:YES];
        return NO;
    }
    if ([url containsString:@"action=12"]) {
        InvestSuccessViewController *isVC = [[InvestSuccessViewController alloc]init];
        NSRange borrowIdRange = [url rangeOfString:@"borrowId="];
        NSString *str1 = [url substringFromIndex:borrowIdRange.location+borrowIdRange.length];
        NSRange andRange = [str1 rangeOfString:@"&"];
        NSString *borrowId = [str1 substringToIndex:andRange.location];
        if ([url containsString:@"tenderId"]) {
            NSRange tenderRange = [url rangeOfString:@"tenderId="];
            NSString *tenderId = [url substringFromIndex:tenderRange.location+tenderRange.length];
            isVC.tenderId = tenderId;
        }else if ([url containsString:@"rechargeId"]){
            //@"xspcf://xspcf?action=12&borrowId=500206&rechargeId=770"
            NSRange rechargeRange = [url rangeOfString:@"rechargeId="];
            NSString *rechargeId = [url substringFromIndex:rechargeRange.location+rechargeRange.length];
            isVC.rechargeId = rechargeId;
        }

        
        isVC.borrowId = borrowId;
        [self presentViewController:isVC animated:YES completion:^{
            [self.navigationController popViewControllerAnimated:YES];
//            [self.navigationController popToRootViewControllerAnimated:YES];
            SunMyOrderVC *orderVC = [[SunMyOrderVC alloc]init];
            [self.navigationController pushViewController:orderVC animated:YES];
        }];
        return NO;
    }
    if ([url hasSuffix:@"action=10"]) {
        switch (self.type) {
//            case Recharge:{
//                SunRecordVC *recordVC = [[SunRecordVC alloc]init];
//                recordVC.type = 2;
//                [self.navigationController popViewControllerAnimated:YES];
//                [self.navigationController pushViewController:recordVC animated:YES];
//                return NO;
//                break;
//            }
//            case GetCash:{
//                SunRecordVC *recordVC = [[SunRecordVC alloc]init];
//                recordVC.type = 1;
//                [self.navigationController popViewControllerAnimated:YES];
//                [self.navigationController pushViewController:recordVC animated:YES];
//                return NO;
//                break;
//            }
            case OpenBind:{
                OpenBindSuccessViewController *vc = [[OpenBindSuccessViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                return NO;
                break;
            }
//            case MakeVerb:{
//                SunMyOrderVC *orderVC = [[SunMyOrderVC alloc]init];
//                [self.navigationController popViewControllerAnimated:YES];
//                [self.navigationController pushViewController:orderVC animated:YES];
//                return NO;
//                break;
//            }
            default:
                break;
        }

    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [[Sun shareSun]hideAll];
    if (!self.title) {
        self.title = [_web stringByEvaluatingJavaScriptFromString:@"document.title"];
    }
}

- (BOOL)navigationShouldPopOnBackButton{
    if (_web.canGoBack) {
        [_web goBack];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
